package com.aiit.controller;

import com.aiit.pojo.*;
import com.aiit.service.*;
import com.aiit.util.MD5utils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/main/manage")
public class ManageController {
    @Autowired
    ILingJianService lingJianService;
    @Autowired
    IUserService userService;
    @Autowired
    IMerchantService merchantService;
    @Autowired
    ICustomerService customerService;
    @Autowired
    ICustomerAddressService customerAddressService;
    @RequestMapping("/manage")
    public String toManage(){
        return "/main/manage/manage";
    }
    //查找页面查找栏相关信息
    @ResponseBody
    @RequestMapping("/select/manage")
    public Map<String,Object> selectManage(){
        Map<String, Object> map = new HashMap<>();
        List<User> users = userService.queryAll1();
        map.put("users",users);
        List<Customer> customers = customerService.queryAll();
        map.put("customers",customers);
        List<LingJian> lingJians = lingJianService.queryAll();
        map.put("lingJians",lingJians);
        List<Merchant> merchants = merchantService.queryAll();
        map.put("merchants",merchants);
        return map;
    }
    //查找用户的相关信息
    @ResponseBody
    @RequestMapping("/select/users")
    public Map<String,Object> selectUsers(@RequestParam("id")String id,@RequestParam("name")String name){
        Map<String, Object> map = new HashMap<>();
        if (!(id.equals("直接选择或搜索"))){
            User user = userService.getById(Integer.parseInt(id));
            map.put("user",user);
            map.put("result",1);
        }else if (!(name.equals("直接选择或搜索"))){
            User user = userService.getOne(new QueryWrapper<User>().eq("name", name));
            map.put("user",user);
            map.put("result",1);
        }else {
            List<User> users = userService.queryAll1();
            map.put("users",users);
            map.put("result",2);
        }
        return map;
    }
    //删除用户相关信息
    @ResponseBody
    @RequestMapping("/delete/users")
    public Integer deleteUsers(@RequestParam("id")Integer id){
        if (userService.removeById(id)){
            return 1;
        }else {
            return 0;
        }
    }
    //查找客户相关信息
    @ResponseBody
    @RequestMapping("/select/customers")
    public Map<String,Object> selectCustomers(@RequestParam("id")String id,@RequestParam("name")String name){
        Map<String, Object> map = new HashMap<>();
        List<CustomerAddress> customerAddresses = customerAddressService.queryAll();
        if (!(id.equals("直接选择或搜索"))){
            Customer customer = customerService.getById(Integer.parseInt(id));
            map.put("customer",customer);
            map.put("result",1);
        }else if (!(name.equals("直接选择或搜索"))){
            Customer customer = customerService.getOne(new QueryWrapper<Customer>().eq("name", name));
            map.put("customer",customer);
            map.put("result",1);
        }else {
            List<Customer> customers = customerService.queryAll();
            map.put("customers",customers);
            map.put("result",2);
        }
        map.put("customerAddresses",customerAddresses);
        return map;
    }
    //删除客户相关信息
    @ResponseBody
    @RequestMapping("/delete/customers")
    public Integer deleteCustomer(@RequestParam("id")Integer id){
        if (customerService.removeById(id)){
            return 1;
        }else {
            return 0;
        }
    }
    //查找客户相关信息
    @ResponseBody
    @RequestMapping("/select/lingjains")
    public Map<String,Object> selectLingJian(@RequestParam("id")String id,@RequestParam("name")String name){
        Map<String, Object> map = new HashMap<>();
        if (!(id.equals("直接选择或搜索"))){
            LingJian lingJian = lingJianService.getById(Integer.parseInt(id));
            map.put("lingJian",lingJian);
            map.put("result",1);
        }else if (!(name.equals("直接选择或搜索"))){
            LingJian lingJian = lingJianService.getOne(new QueryWrapper<LingJian>().eq("name", name));
            map.put("lingJian",lingJian);
            map.put("result",1);
        }else {
            List<LingJian> lingJians = lingJianService.queryAll();
            map.put("lingJians",lingJians);
            map.put("result",2);
        }
        return map;
    }
    //删除客户相关信息
    @ResponseBody
    @RequestMapping("/delete/lingjians")
    public Integer deleteLingJian(@RequestParam("id")Integer id){
        if (lingJianService.removeById(id)){
            return 1;
        }else {
            return 0;
        }
    }
    //查找供货商相应信息
    @ResponseBody
    @RequestMapping("/select/merchants")
    public Map<String,Object> selectMerchant(@RequestParam("id")String id,@RequestParam("name")String name){
        Map<String, Object> map = new HashMap<>();
        if (!(id.equals("直接选择或搜索"))){
            Merchant merchant = merchantService.getById(Integer.parseInt(id));
            map.put("merchant",merchant);
            map.put("result",1);
        }else if (!(name.equals("直接选择或搜索"))){
            Merchant merchant = merchantService.getOne(new QueryWrapper<Merchant>().eq("name", name));
            map.put("merchant",merchant);
            map.put("result",1);
        }else {
            List<Merchant> merchants = merchantService.queryAll();
            map.put("merchants",merchants);
            map.put("result",2);
        }
        return map;
    }
    //删除用户相关信息
    @ResponseBody
    @RequestMapping("/delete/merchants")
    public Integer deleteMerchants(@RequestParam("id")Integer id){
        if (merchantService.removeById(id)){
            return 1;
        }else {
            return 0;
        }
    }
    @RequestMapping("/toinsertuser")
    public String toInsertUser(){
        return "/main/manage/insertuser";
    }
    //增加新用户
    @ResponseBody
    @RequestMapping("/insert/user")
    public Integer insertUser(@RequestParam("name")String name,@RequestParam("username")String username,@RequestParam("password")String password,@RequestParam("department")String department){
        User user = new User();
        user.setName(name);
        user.setUsername(username);
        String newPassWord = MD5utils.code(password);
        user.setPassword(newPassWord);
        user.setEntryTime(new Date());
        if (department.equals("采购部门")){
            user.setDepartmentId(1);
        }else if (department.equals("销售部门")){
            user.setDepartmentId(2);
        }
        List<User> users = userService.queryAll();
        for (int i = 0;i<users.size();i++){
            if (users.get(i).getName().equals(name)){
                return 2;
            }
        }
        if (userService.saveOrUpdate(user)){
            return 1;
        }
        return 0;
    }
    @RequestMapping("/toinsertcustomer")
    public String toInsertCustomer(){
        return "/main/manage/insertcustomer";
    }
    //添加新顾客
    @ResponseBody
    @RequestMapping("/insert/customer")
    public Integer insertCustomer(@RequestParam("name")String name,@RequestParam("phone")String phone,@RequestParam("address")String address){
        Customer customer = new Customer();
        customer.setName(name);
        customer.setPhone(phone);
        List<Customer> customers = customerService.queryAll();
        for (int i = 0;i<customers.size();i++){
            if (customers.get(i).getName().equals(name)){
                return 2;
            }
        }
        if (customerService.saveOrUpdate(customer)){
            CustomerAddress customerAddress = new CustomerAddress();
            customerAddress.setCustomerId(customerService.getOne(new QueryWrapper<Customer>().eq("name",name)).getId());
            customerAddress.setAddress(address);
            if (customerAddressService.saveOrUpdate(customerAddress)){
                return 1;
            }
            return 0;
        }
        return 0;
    }
    @RequestMapping("/toinsertlingjian")
    public String toInsertLingJian(){
        return "/main/manage/insertlingjian";
    }
    //添加新商品
    @ResponseBody
    @RequestMapping("/insert/lingjian")
    public Integer insertLingJian(@RequestParam("name")String name,@RequestParam("price")Double price){
        List<LingJian> lingJians = lingJianService.queryAll();
        for (int i = 0;i<lingJians.size();i++){
            if (lingJians.get(i).getName().equals(name)){
                return 2;
            }
        }
        LingJian lingJian = new LingJian();
        lingJian.setName(name);
        lingJian.setPrice(price);
        if (lingJianService.saveOrUpdate(lingJian)){
            return 1;
        }
        return 0;
    }
    @RequestMapping("/toinsertmerchant")
    public String toInsertMerchant(){
        return "/main/manage/insertmerchant";
    }
    //添加新供货商
    @ResponseBody
    @RequestMapping("/insert/merchant")
    public Integer insertMerchant(@RequestParam("name")String name,@RequestParam("address")String address){
        List<Merchant> merchants = merchantService.queryAll();
        for (int i = 0;i<merchants.size();i++){
            if (merchants.get(i).getName().equals(name)){
                return 2;
            }
        }
        Merchant merchant = new Merchant();
        merchant.setName(name);
        merchant.setAddress(address);
        if (merchantService.saveOrUpdate(merchant)){
            return 1;
        }
        return 0;
    }
}
